package com.smm.web.vmc.controller.admin;

import com.smm.web.vmc.domain.Camera;
import com.smm.web.vmc.domain.Warehouse;
import com.smm.web.vmc.dto.CameraDto;
import com.smm.web.vmc.dto.JsonReturn;
import com.smm.web.vmc.service.CameraService;
import com.smm.web.vmc.service.WarehouseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhenghao on 2016/9/7.
 *
 *
 */
@Controller
@RequestMapping("/admin/camera")
public class CameraController {

    @Resource
    private WarehouseService warehouseService;

    @Resource
    private CameraService cameraService;


    @RequestMapping("/listByWh.do")
    public ModelAndView listByWh(int whId){

        ModelAndView mv = new ModelAndView("admin/camera/list");

        Warehouse warehouse = warehouseService.getOneById(whId);

        //获取指定仓库所有摄像头列表
        List<Camera> cameraList = cameraService.getAllByWarehouse(whId);

        mv.addObject("warehouse",warehouse);
        mv.addObject("cameraList",cameraList);

        return mv;
    }


    @RequestMapping("/add.do")
    public ModelAndView add(int whId){

        ModelAndView mv = new ModelAndView("admin/camera/add");

        Warehouse warehouse = warehouseService.getOneById(whId);

        mv.addObject("warehouse",warehouse);


        return mv;
    }


    @RequestMapping("/submitAdd.do")
    @ResponseBody
    public JsonReturn submitAdd(CameraDto cameraDto){

        try{

            cameraService.add(cameraDto);

        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,"新增失败！"+e.getMessage());
        }


        return new JsonReturn(1,"新增成功");
    }



    @RequestMapping("/update.do")
    public ModelAndView update(int whId,int cameraId){

        ModelAndView mv = new ModelAndView("admin/camera/update");

        Warehouse warehouse = warehouseService.getOneById(whId);

        //获取摄像头信息
        Camera camera = cameraService.getOneById(cameraId);

        mv.addObject("camera",camera);
        mv.addObject("warehouse",warehouse);

        return mv;
    }


    @RequestMapping("/submitUpdate.do")
    @ResponseBody
    public JsonReturn submitUpdate(CameraDto cameraDto){

        try{

            cameraService.update(cameraDto);

        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,"修改失败！"+e.getMessage());
        }


        return new JsonReturn(1,"修改成功");
    }



    @RequestMapping("/del.do")
    @ResponseBody
    public JsonReturn del(int id){

        try{
            cameraService.deleteById(id);

        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,"删除失败！"+e.getMessage());
        }

        return new JsonReturn(1,"删除成功");
    }



    @RequestMapping("/show.do")
    public ModelAndView show(int whId,int cameraId){

        ModelAndView mv = new ModelAndView("admin/camera/show");

        Warehouse warehouse = warehouseService.getOneById(whId);

        //获取摄像头信息
        Camera camera = cameraService.getOneById(cameraId);

        mv.addObject("camera",camera);
        mv.addObject("warehouse",warehouse);

        return mv;
    }


    @RequestMapping("/createCapture.do")
    @ResponseBody
    public JsonReturn createCapture(Camera camera){

        try{

            if(camera==null) throw new Exception("摄像头数据不存在");

            String imageUrl=cameraService.captureByAPI(camera);

            return new JsonReturn(1,"抓拍成功",imageUrl);

        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,"抓拍失败！"+e.getMessage());
        }

    }


}
